// // Copyright (c) 2009 All Right Reserved // // Stephen Toub // stoub@microsoft.com // 2009-01-01 // Contains ... using System; using System.IO; using System.Text; using JetBrains.Annotations; using LargoCommon.Abstract; using LargoCommon.Music; namespace LargoCommon.Midi { /// Represents a voice category message that deals with notes. [Serializable] public abstract class VoiceAbstractNote : VoiceEvent { #region Fields /// The MIDI note to modify (0x0 to 0x7F). private byte note; #endregion #region Constructors /// Initializes a new instance of the VoiceAbstractNote class. /// The amount of time before this event. /// The category identifier (0x0 through 0xF) for this voice event. /// The channel (0x0 through 0xF) for this voice event. /// The MIDI note to modify (0x0 to 0x7F). protected VoiceAbstractNote(long deltaTime, byte givenCategory, MidiChannel channel, byte note) : base(deltaTime, givenCategory, channel) { this.Note = note; } /// Initializes a new instance of the VoiceAbstractNote class. /// The amount of time before this event. /// The category identifier (0x0 through 0xF) for this voice event. /// The channel (0x0 through 0xF) for this voice event. [UsedImplicitly] protected VoiceAbstractNote(long deltaTime, byte givenCategory, MidiChannel channel) : base(deltaTime, givenCategory, channel) { } #endregion #region Properties /// Gets or sets the MIDI note (0x0 to 0x7F). /// General musical property. public byte Note { get => this.note; set { if (value > 127) { while (value > 127) { value -= DefaultValue.HarmonicOrder; } //// // throw new ArgumentOutOfRangeException("Note", value, "The note must be in the range from 0 to 127."); } this.note = value; } } #endregion #region Protected Properties /// The first parameter as sent in the MIDI message. /// General musical property. public sealed override byte Parameter1 => this.note; #endregion #region To String /// Generate a string representation of the event. /// A string representation of the event. public override string ToString() { var sb = new StringBuilder(); if (this.Channel == MidiChannel.DrumChannel && Enum.IsDefined(typeof(MidiRhythmicInstrument), this.note)) { sb.Append((MidiRhythmicInstrument)this.note); // print out percussion name } else { var note1 = MusicalProperties.GetNoteNameAndOctave(this.note, DefaultValue.HarmonicOrder); sb.Append(note1); // print out note name } sb.Append(" \t" + base.ToString()); sb.Append(" \t"); return sb.ToString(); } #endregion #region Methods /// Write the event to the output stream. /// The stream to which the event should be written. public override void Write(Stream outputStream) { if (outputStream == null) { return; } //// Write out the base event information base.Write(outputStream); //// Write out the data outputStream.WriteByte(this.note); } #endregion } }